<?php
/*
* 
**/

function check_valid_user ()
{
	if (!isset($_SESSION["loadname"]))
	{
		//show or link to log in page.
		exit("Please log in.");
	}
}

function connect_db ()
{
	$db_name =  "vrwebmonitor";
	$dbcnx = @mysql_connect("localhost","root","bwu929as");
	if (!$dbcnx)
	{
		echo "ÎÞ·¨Á¬½ÓÊý¾Ý¿â£¬ÇëÉÔºóÔÙÊÔ!<br>";
		echo '<p><a href="1.html">·µ»ØµÇÂ½½çÃæ</a>';
		exit();
	}

	if (!@mysql_select_db("$db_name", $dbcnx) )
	{
		echo "ÎÞ·¨Á¬½ÓÊý¾Ý¿â£¬ÇëÉÔºóÔÙÊÔ!<br>";
		echo '<p><a href="1.html">·µ»ØµÇÂ½½çÃæ</a>';
		exit();
	}
	return $dbcnx;
}

function socket_connection ()
{
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if ($socket === false)
	{
		echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
	}
	$result = socket_connect($socket, '192.168.237.217', 3000);
	if ($result === false)
	{
		echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
	}
	return $socket;
}

/*
* vlink_time_ctrl_msg_pack
* Vlink控制报文的封装函数
* 各参数含义详情见《Web服务器与伺服系统通讯格式.doc》 
* 返回值buf是已封装好的单个控制报文及时间
**/

function vlink_time_ctrl_msg_pack ($vlinkid, $vlinkstate, $vripv6addr, $interval)
{
	$type = pack("C", 2);
	$oper = pack("C", 0);
	$vid = pack("C", $vlinkid);
	if ( strtolower(trim($vlinkstate)) == "up")
	{
		$vlinkstate = 1;
	}
	else if ( strtolower(trim($vlinkstate)) == "down")
	{
		$vlinkstate = 0;
	}
	$vs = pack("C", $vlinkstate);
	$ipv6addr = inet_pton($vripv6addr);
	$ti = pack("I", $interval);
	$buf = $type.$oper.$vid.$vs.$ipv6addr.$ti;
	return $buf;
}

/*
* ctrl_vlink_by_time_script_parse
* 返回值是已封装好的Vlink定时控制报文，可以直接通过socket发送
**/
function ctrl_vlink_by_time_script_parse ($xml)
{
	$xml = new SimpleXMLElement($xml);
	$msg .= pack("C", 8);
	//echo "nihao" . $msg;
	$i = 0;
	foreach ($xml->vr as $vr)
	{
		$ipv6addr = $vr["ipv6addr"];
		//echo $ipv6addr;
		foreach ($vr->vlink as $vlink)
		{
			$buf .= vlink_time_ctrl_msg_pack($vlink["vlinkno"],
					$vlink->statectrl,
					$ipv6addr,
					$vlink->time);
			$i++;
		}
	}
	//echo $i;
	$msg .= pack("C", $i) . $buf;
	return $msg;
}

function vlink_param_msg_pack ($vlinkid, $interval, $locipv4, $oppipv4, $oppipv6)
{
	//$p_type = pack("C", 4);
	//$p_oper = pack("C", $oper);
	$p_vlinkid = pack("C", $vlinkid);
	//machine dependent size and byte order
	$p_interval = pack("I", $interval);
	$p_locipv4 = inet_pton($locipv4);
	$p_oppipv4 = inet_pton($oppipv4);
	$p_oppipv6 = inet_pton($oppipv6);
	//$p_locipv6 = inet_pton($locipv6);
	$buf = $p_vlinkid.$p_interval.$p_locipv4.$p_oppipv4.$p_oppipv6;
	return $buf;
}

function vlink_add_script_parse ($xml)
{
	$xml = new SimpleXMLElement($xml);
	$type = pack ("C", 4);
	$oper = pack ("C", 1);
	$socket = socket_connection();

	foreach ($xml->vr as $vr)
	{
		$ipv6addr = $vr["ipv6addr"];
		$n = 0;
		foreach ($vr->vlink as $vlink)
		{
			$vlink_param .= vlink_param_msg_pack($vlink["vlinkno"],
								$vlink->hellointerval,
								$vlink->locipv4addr,
								$vlink->oppipv4addr,
								$vlink->oppipv6addr);
			$n++;
		}
		$msg = $type . $oper . pack("C", $n) . $vlink_param . inet_pton($ipv6addr);
		$len = strlen($msg);
		socket_write($socket, $msg, $len);
	}
	socket_close($socket);
}

function vlink_conf_script_parse ($xml, $operation_type = "add")
{
	$xml = new SimpleXMLElement($xml);
	$type = pack ("C", 4);
	if ($operation_type == "add")
	{
		$oper = pack ("C", 1);
	}
	else if ($operation_type == "del")
	{
		$oper = pack ("C", 3);
	}
	else
	{
		echo "The second parameter nonidentifiable.\n";
		exit(1);
	}
	$socket = socket_connection();

	foreach ($xml->vr as $vr)
	{
		$ipv6addr = $vr["ipv6addr"];
		$n = 0;
		foreach ($vr->vlink as $vlink)
		{
			$vlink_param .= vlink_param_msg_pack($vlink["vlinkno"],
							  $vlink->hellointerval,
							  $vlink->locipv4addr,
							  $vlink->oppipv4addr,
							  $vlink->oppipv6addr);
			$n++;
		}
		$msg = $type . $oper . pack("C", $n) . $vlink_param . inet_pton($ipv6addr);
		$len = strlen($msg);
		socket_write($socket, $msg, $len);
	}
	socket_close($socket);
}

/*
* 删除指定用户的所有Vlink
**/
function delete_user_all_vlinks ($username)
{
	$dbcnx = connect_db();
	$socket = socket_connection();
	//查找，并按VR排序
	$query = "select * from vrlinkstate where username = " . $username . " order by \"vraddr\" DESC;";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	$pre_vraddr;
	$msg = pack("C", 4) . pack("C", 2);
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$cur_vraddr = $line["vraddr"];
		if (isset($pre_vraddr) && ($pre_vraddr != $cur_vraddr))
		{
			$msg .= inet_pton($per_vraddr);
			$len = strlen($msg);
			socket_write($socket, $msg, $len);

			$msg = pack("C", 4) . pack("C", 2);
		}
		$pre_vraddr = $cur_vraddr;
		$msg .= vlink_param_msg_pack($line["vlinkid"],
					$line["sendinterval"],
					$line["locipv4"],
					$line["oppipv4"],
					$line["oppipv6"]);
	}
	socket_close($socket);
	mysql_close($dbcnx);
}

function topol_recover_script_parse ($xml, $username)
{
	delete_user_all_vlinks($username);
	$dbcnx = connect_db();
	$query = "select * from vrlinkstate where username = " . $username . ";";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	if (mysql_affected_rows() > 0)
	{
		delete_user_all_vlinks($username);
	}
	vlink_conf_script_parse($xml, "add");
	mysql_close($dbcnx);
}

function export_xml ($username)
{
	$dbcnx = connect_db();
	if ("root" == $username)
	{
		$query = "select * from vrlinkstate order by \"vraddr\" DESC;";
	}
	else
	{
		$query = "select * from vrlinkstate where username = " . $username . " order by \"vraddr\" DESC;";	
	}
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	$pre_vraddr;
	$vlink_conf_xml .= "<vlinkconf>\r\n";
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$cur_vraddr = $line["vraddr"];
		if (!isset($pre_vraddr))
		{
			$vlink_conf_xml .= "\t<vr ipv6addr=\"" . $cur_vraddr . "\">\r\n";
		}
		else if ($pre_vraddr != $cur_vraddr)
		{
			$vlink_conf_xml .= "\t</vr>\r\n";
			$vlink_conf_xml .= "\t<vr ipv6addr=\"" . $cur_vraddr . "\">\r\n";
		}
		$pre_vraddr = $cur_vraddr;
		$vlink_conf_xml .= "\t\t<vlink vlinkno=\"" . $line["vlinkid"] . "\">\r\n";
		$vlink_conf_xml .= "\t\t\t<locipv4addr>" . $line["locipv4"] . "</locipv4addr>\r\n";
		$vlink_conf_xml .= "\t\t\t<oppipv4addr>" . $line["oppipv4"] . "</oppipv4addr>\r\n";
		$vlink_conf_xml .= "\t\t\t<oppipv6addr>" . $line["oppipv6"] . "</oppipv6addr>\r\n";
		$vlink_conf_xml .= "\t\t\t<hellointerval>" . $line["sendinterval"] . "</hellointerval>\r\n";
		$vlink_conf_xml .= "\t\t</vlink>\r\n";
	}
	$vlink_conf_xml .= "\t</vr>\r\n";
	$vlink_conf_xml .= "</vlinkconf>\r\n";
	mysql_close($dbcnx);
	return $vlink_conf_xml;
}

?>


